<template>
  <div>
    <el-row style="margin-bottom:20px;display:flex;justify-content:flex-start">
      <el-input placeholder="车辆编号" style="width:20%;margin-right:10px" v-model="carid" clearable></el-input>
      <el-input placeholder="车辆牌照" style="width:20%;margin-right:10px" v-model="carno" clearable></el-input>
      <!--<selectcarnumber
        placeholder="牌照"
        @getpersonid2="getpersonid2"
        :itemtype="2"
        style="width:20%;margin-right:10px"
      ></selectcarnumber>-->
      <!-- <el-input placeholder="牌照" style="width:20%;margin-right:10px" v-model="carno" clearable></el-input> -->
      <selectorg @getselectorgid="getselectorgid" style="width:20%;margin-right:10px" clearable></selectorg>
      <el-input placeholder="负责人姓名" style="width:20%;margin-right:10px" v-model="holder" clearable></el-input>
      <!-- <searchperson @getpersonid="getpersonid" style="width:20%;margin-right:10px"></searchperson> -->
      <el-date-picker
        v-model="paytime"
        style="width:32%;margin-right:20px"
        type="daterange"
        rangeSeparator="至"
        startPlaceholder="开始日期"
        endPlaceholder="结束日期"
        valueFormat="yyyy-MM-dd"
        clearable
      >
      </el-date-picker>
      <!-- <el-date-picker
        v-model="paytime"
        type="date"
        style="margin-right:10px"
        placeholder="购置日期"
        value-format="yyyy-MM-dd"
      ></el-date-picker> -->

      <el-button size="mini" type="primary" @click="getcarlist">搜索</el-button>
    </el-row>

    <el-row>
      <el-button size="mini" @click="showaddcar">添加车辆信息</el-button>
      <el-button size="mini" type="primary" @click="amandcar()">编辑</el-button>
      <el-button size="mini" type="success" @click="deletecar()">删除</el-button>
    </el-row>
    <el-divider></el-divider>
    <cartable
      :tableData="carlist"
      @itemvals="itemvals"
      :pagobj.sync="pagobj"
      :itemval1.sync="itemval1"
    ></cartable>

    <addcar
      v-if="isaddcar"
      :addcar.sync="changelist"
      :itemvalname="itemvalname"
      :chousebtn="btnvalue"
    ></addcar>
  </div>
</template>

<script>
import addcar from '~/components/carmanager/carlist/addcar.vue';
import cartable from '~/components/carmanager/carlist/cartable.vue';
import selectorg from '~/components/common/selectorg.vue';
import selectcarnumber from '~/components/common/selectcarnumber.vue';

export default {
  components: {
    cartable,
    addcar,
    selectorg,
    selectcarnumber,
  },
  data() {
    return {
      itemvalname: {},
      caridarr: [], // 删除所用的数组id
      isaddcar: false, // 添加弹窗
      isaddcar2: false, // 编辑弹窗
      ts: this.newStore(),
      carlist: [],
      btnvalue: 0,
      itemval1: [], // 从表格获取点击的次数
      holderid: '', // 负责人
      branchid: '', // 部门
      carid: '', // 车辆编号
      holder: '', // name
      carno: '', // 牌照
      paytime: ['', ''], // 购置日期
      itemval: {}, // 编辑传回来的d对象
      Object: '',
      changelist: '',
      pagobj: {
        currentPage3: 1,
        pagesize: 10,
        total: 10,
      },
    };
  },
  watch: {
    Object() {
      this.getcarlist();
    },
    changelist() {
      this.getcarlist();
    },
  },
  created() {
    this.getcarlist();
  },
  methods: {
    // 获取cardid
    getpersonid2(val) {
      // this.carid = val.id;
      this.carno = val.id;
    },
    // 删除车辆信息
    deletecar() {
      // console.log(this.itemval1, "0222222");

      if (this.itemval1.length > 1) {
        this.$confirm(`是否删除${this.itemval1.length}条车辆信息?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const req = {
            handle: '0E56125B90D2898AF7068E625DF3AFE2479C1A22417B4081',
            carid: this.caridarr,
          };
          this.spost(this.ts, '/mainservlet', req).then((ts) => {
            if (ts.d.sflag == 'success') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
              this.getcarlist();
              // this.$emit("addcar", this.branchidlist);
            }
          });
        });
      } else if (this.itemval1.length == 1) {
        this.$confirm('是否删除该车辆信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const req = {
            handle: '0E56125B90D2898AF7068E625DF3AFE2479C1A22417B4081',
            carid: this.caridarr,
          };
          this.spost(this.ts, '/mainservlet', req).then((ts) => {
            if (ts.d.sflag == 'success') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
              this.getcarlist();
              // this.$emit("addcar", this.branchidlist);
            }
          });
        });
      } else {
        this.$alert('请选择车辆', '提示');
      }
    },
    // 编辑车辆信息
    amandcar() {
      // console.log(this.itemval1, "-------");

      this.btnvalue = 2;
      if (this.itemval1.length == 1) {
        let itemvals = {};
        itemvals = this.itemval[0];
        this.itemvalname = itemvals;
        // console.log(this.itemvalname,'44')
        this.reload('isaddcar');
      } else if (this.itemval1.length == 0) {
        this.$message('请先勾选一个车辆信息');
      } else {
        this.$message('编辑只能编辑一个车辆信息');
      }
    },
    // table传给父组件
    itemvals(val) {
      // console.log(val, "wwwwww");
      this.itemval = val;
      this.caridarr = [];
      for (let i = 0; i < this.itemval1.length; i++) {
        this.caridarr.push(this.itemval1[i].hash.carid);
      }
      // console.log(this.caridarr, "23");
    },

    // 监听子组件的(添加)
    addcar(val) {
      this.Object = {};
      this.Object = val;

      // val.hash.holder = val.hash.holders;
      // console.log(val, "22223322");
      // this.carlist.unshift(val);
      // console.log(this.carlist, "66666");
    },
    // 获取负责人
    getpersonid(val) {
      // console.log();
      this.holder = val.id;
    },
    // 获取部门
    getselectorgid(val) {
      this.branchid = val;
    },
    // 公共组件刷新
    reload(partsname) {
      // console.log(partsname);

      this[partsname] = false;
      this.$nextTick(() => {
        this[partsname] = true;
      });
    },
    // 点击添加车辆
    showaddcar() {
      this.btnvalue = 1;
      this.reload('isaddcar');
    },
    // 获取列表
    getcarlist() {
      // console.log("2222");
      const req = {
        handle: '0E56125B90D2898AFCC52E33BD59EA5BF6486B1D96ED9F7E',
        carid: this.carid,
        carno: this.carno,
        branchid: this.branchid,
        holder: this.holder,
        paytimes: this.paytime[0],
        paytimee: this.paytime[1],
        start: (this.pagobj.currentPage3 - 1) * this.pagobj.pagesize,
        limit: this.pagobj.pagesize,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        if (ts.d.sflag == 'success') {
          this.carlist = ts.d.data.operation1;
          this.pagobj.total = parseInt(ts.d.data.operation2[0].hash.allrs);
        }
      });
    },
  },
};
</script>

